﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.SysTools;
using System.Data.SqlClient;
using System.Data;
using System.Text;

namespace GovernmentOfficeProject.LevelDataAccess
{
    public class DAL_SYS_Privilege
    {
        public Entity_SYS_Privilege getPrivilegeByPrivilegeID(decimal priID)
        {
            try
            {
                Entity_SYS_Privilege privilege = null;
                String strSQL = "select * from [SYS_PRIVILEGE] where PriID=@priID";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@priID",priID)
                };
                DataSet dst = DAL_DBGM.GetDataSet(strSQL, values);
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    privilege = new Entity_SYS_Privilege();
                    privilege.PriID = Convert.ToDecimal(row["PriID"]);
                    privilege.PriMaster = Convert.ToString(row["PriMaster"]);
                    privilege.PriMasterValue = Convert.ToDecimal(row["PriMasterValue"]);
                    privilege.PriAccess = Convert.ToString(row["PriAccess"]);
                    privilege.PriAccessValue = Convert.ToDecimal(row["PriAccessValue"]);
                    privilege.PriOperation = Convert.ToString(row["PriOperation"]);
                    privilege.CreateUserID = Convert.ToDecimal(row["PriCreateUserID"]);
                    privilege.CreateDateTime = Convert.ToString(row["PriCreateDateTime"]);
                    privilege.ModifyUserID = Convert.ToDecimal(row["PriModifyUserID"]);
                    privilege.ModifyDateTime = Convert.ToString(row["PriModifyDateTime"]);
                    privilege.RecordStatus = Convert.ToDecimal(row["PriRecordStatus"]);
                }
                return privilege;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_SYS_Privilege.getPrivilegeByPrivilegeID(decimal priID)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个private实体,调用处必须确定参数值仅返回一行数据.
        /// </summary>
        /// <param name="priEntity"></param>
        /// <returns></returns>
        public Entity_SYS_Privilege getPrivilegeByPrivilegeEntity(Entity_SYS_Privilege priEntity)
        {
            try
            {
                String strSQL = "select * from [SYS_PRIVILEGE] where 1=1";

                if (priEntity.PriMaster != "" && priEntity.PriMaster != null)
                {
                    strSQL += " and PriMaster='" + priEntity.PriMaster + "'";
                }
                if (priEntity.PriMasterValue != -1)
                {
                    strSQL += " and PriMasterValue=" + priEntity.PriMasterValue;
                }
                if (priEntity.PriAccess != "" && priEntity.PriAccess != null)
                {
                    strSQL += " and PriAccess='" + priEntity.PriAccess + "'";
                }
                if (priEntity.PriAccessValue != -1)
                {
                    strSQL += " and PriAccessValue=" + priEntity.PriAccessValue;
                }
                if (priEntity.PriOperation != "" && priEntity.PriOperation != null)
                {
                    strSQL += " and PriOperation='" + priEntity.PriOperation + "'";
                }
                if (priEntity.CreateUserID != -1)
                {
                    strSQL += " and PriCreateUserID=" + priEntity.CreateUserID;
                }
                if (priEntity.CreateDateTime != "" && priEntity.CreateDateTime != null)
                {
                    strSQL += " and PriCreateDateTime = '" + priEntity.CreateDateTime + "'";
                }
                if (priEntity.ModifyUserID != -1)
                {
                    strSQL += " and PriModifyUserID=" + priEntity.ModifyUserID;
                }
                if (priEntity.ModifyDateTime != "" && priEntity.ModifyDateTime != null)
                {
                    strSQL += " and PriModifyDateTime='" + priEntity.ModifyDateTime + "'";
                }
                if (priEntity.RecordStatus != -1)
                {
                    strSQL += " and PriRecordstatus=" + priEntity.RecordStatus;
                }
                //AppLog.Write("DAL-sql:" + strSQL, AppLog.LogMessageType.Debug);
                DataSet dst = DAL_DBGM.GetDataSet(strSQL);
                Entity_SYS_Privilege privilege = new Entity_SYS_Privilege();
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    privilege.PriID = Convert.ToDecimal(row["PriID"]);
                    //AppLog.Write(privilege.PriID.ToString(), AppLog.LogMessageType.Debug);
                    privilege.PriMaster = Convert.ToString(row["PriMaster"]);
                    privilege.PriMasterValue = Convert.ToDecimal(row["PriMasterValue"]);
                    privilege.PriAccess = Convert.ToString(row["PriAccess"]);
                    privilege.PriAccessValue = Convert.ToDecimal(row["PriAccessValue"]);
                    privilege.PriOperation = Convert.ToString(row["PriOperation"]);
                    privilege.CreateUserID = Convert.ToDecimal(row["PriCreateUserID"]);
                    privilege.CreateDateTime = Convert.ToString(row["PriCreateDateTime"]);
                    privilege.ModifyUserID = Convert.ToDecimal(row["PriModifyUserID"]);
                    privilege.ModifyDateTime = Convert.ToString(row["PriModifyDateTime"]);
                    privilege.RecordStatus = Convert.ToDecimal(row["PriRecordStatus"]);
                }
                return privilege;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_SYS_Privilege.getPrivilegeByPrivilegeEntity(Entity_SYS_Privilege priEntity)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }

        public List<Entity_SYS_Privilege> getPrivilegeList()
        {
            try
            {   String strSQL = "select * from [SYS_PRIVILEGE]";
                List<Entity_SYS_Privilege> privilegeList = new List<Entity_SYS_Privilege>();                
                DataSet dst = DAL_DBGM.GetDataSet(strSQL);
                Entity_SYS_Privilege privilege = null;
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    privilege = new Entity_SYS_Privilege();
                    privilege.PriID = Convert.ToDecimal(row["PriID"]);
                    privilege.PriMaster = Convert.ToString(row["PriMaster"]);
                    privilege.PriMasterValue = Convert.ToDecimal(row["PriMasterValue"]);
                    privilege.PriAccess = Convert.ToString(row["PriAccess"]);
                    privilege.PriAccessValue = Convert.ToDecimal(row["PriAccessValue"]);
                    privilege.PriOperation = Convert.ToString(row["PriOperation"]);
                    privilege.CreateUserID = Convert.ToDecimal(row["PriCreateUserID"]);
                    privilege.CreateDateTime = Convert.ToString(row["PriCreateDateTime"]);
                    privilege.ModifyUserID = Convert.ToDecimal(row["PriModifyUserID"]);
                    privilege.ModifyDateTime = Convert.ToString(row["PriModifyDateTime"]);
                    privilege.RecordStatus = Convert.ToDecimal(row["PriRecordStatus"]);
                    privilegeList.Add(privilege);
                }
                return privilegeList;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_SYS_Privilege.getPrivilegeList()---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }

        public List<Entity_SYS_Privilege> getPrivilegeListByPrivilegeEntity(Entity_SYS_Privilege priEntity)
        {
            try
            {
                String strSQL = "select * from [SYS_PRIVILEGE] where 1=1";
                if (priEntity.PriMaster!="" && priEntity.PriMaster!=null)
                {
                    strSQL += " and PriMaster='" + priEntity.PriMaster + "'";
                }
                if (priEntity.PriMasterValue != -1)
                {
                    strSQL += " and PriMasterValue=" + priEntity.PriMasterValue;
                }
                if (priEntity.PriAccess != "" && priEntity.PriAccess != null)
                {
                    strSQL += " and PriAccess='"+priEntity.PriAccess+"'";
                }
                if (priEntity.PriAccessValue != -1)
                {
                    strSQL += " and PriAccessValue=" + priEntity.PriAccessValue;
                }
                if (priEntity.PriOperation!="" && priEntity.PriOperation!=null)
                {
                    strSQL+=" and PriOperation='"+priEntity.PriOperation+"'";
                }
                if (priEntity.CreateUserID != -1)
                {
                    strSQL += " and PriCreateUserID=" + priEntity.CreateUserID;
                }
                if (priEntity.CreateDateTime!="" && priEntity.CreateDateTime!=null)
                {
                    strSQL += " and PriCreateDateTime = '" + priEntity.CreateDateTime + "'";
                }
                if (priEntity.ModifyUserID != -1)
                {
                    strSQL += " and PriModifyUserID=" + priEntity.ModifyUserID;
                }
                if (priEntity.ModifyDateTime!="" && priEntity.ModifyDateTime!=null)
                {
                    strSQL += " and PriModifyDateTime='" + priEntity.ModifyDateTime + "'";
                }
                if (priEntity.RecordStatus != -1)
                {
                    strSQL += " and PriRecordstatus=" + priEntity.RecordStatus;
                }
                //AppLog.Write("DAL-sql:" + strSQL,AppLog.LogMessageType.Debug);
                List<Entity_SYS_Privilege> privilegeList = new List<Entity_SYS_Privilege>();
                DataSet dst = DAL_DBGM.GetDataSet(strSQL);
                Entity_SYS_Privilege privilege = null;
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    privilege = new Entity_SYS_Privilege();
                    privilege.PriID = Convert.ToDecimal(row["PriID"]);
                    privilege.PriMaster = Convert.ToString(row["PriMaster"]);
                    privilege.PriMasterValue = Convert.ToDecimal(row["PriMasterValue"]);
                    privilege.PriAccess = Convert.ToString(row["PriAccess"]);
                    privilege.PriAccessValue = Convert.ToDecimal(row["PriAccessValue"]);
                    privilege.PriOperation = Convert.ToString(row["PriOperation"]);
                    privilege.CreateUserID = Convert.ToDecimal(row["PriCreateUserID"]);
                    privilege.CreateDateTime = Convert.ToString(row["PriCreateDateTime"]);
                    privilege.ModifyUserID = Convert.ToDecimal(row["PriModifyUserID"]);
                    privilege.ModifyDateTime = Convert.ToString(row["PriModifyDateTime"]);
                    privilege.RecordStatus = Convert.ToDecimal(row["PriRecordStatus"]);
                    privilegeList.Add(privilege);
                }                
                return privilegeList;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_SYS_Privilege.getPrivilegeListByPrivilegeEntity(Entity_SYS_Privilege priEntity)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }
        
        public bool addPrivilege(Entity_SYS_Privilege priEntity)
        {
            try
            {
                bool flag = false;
                bool blState = false;
                StringBuilder sqlBudIns = new StringBuilder();
                StringBuilder sqlBudValue = new StringBuilder();
                sqlBudIns.Append("insert into [SYS_PRIVILEGE](");
                sqlBudValue.Append(" values(");
                List<SqlParameter> sprList = new List<SqlParameter>();

                if (priEntity.PriMaster!= "" && priEntity.PriMaster!=null)
                {
                    sqlBudIns.Append("PriMaster,");
                    sqlBudValue.Append("@priMaster,");
                    sprList.Add(new SqlParameter("@priMaster", priEntity.PriMaster));
                    blState = true;
                }
                if (priEntity.PriMasterValue != -1)
                {
                    sqlBudIns.Append("PriMasterValue,");
                    sqlBudValue.Append("@priMasterValue,");
                    sprList.Add(new SqlParameter("@priMasterValue", priEntity.PriMasterValue));
                    blState = true;
                }
                if (priEntity.PriAccess!="" && priEntity.PriAccess!=null)
                {
                    sqlBudIns.Append("PriAccess,");
                    sqlBudValue.Append("@priAccess,");
                    sprList.Add(new SqlParameter("@priAccess", priEntity.PriAccess));
                    blState = true;
                }
                if (priEntity.PriAccessValue != -1)
                {
                    sqlBudIns.Append("PriAccessValue,");
                    sqlBudValue.Append("@priAccessValue,");
                    sprList.Add(new SqlParameter("@priAccessValue", priEntity.PriAccessValue));
                    blState = true;
                }
                if (priEntity.PriOperation!="" && priEntity.PriOperation!=null)
                {
                    sqlBudIns.Append("PriOperation,");
                    sqlBudValue.Append("@priOperation,");
                    sprList.Add(new SqlParameter("@priOperation", priEntity.PriOperation));
                    blState = true;
                }
                if (priEntity.CreateUserID != -1)
                {
                    sqlBudIns.Append("PriCreateUserID,");
                    sqlBudValue.Append("@priCreateUserID,");
                    sprList.Add(new SqlParameter("@priCreateUserID", priEntity.CreateUserID));
                    blState = true;
                }
                if (priEntity.CreateDateTime!="" && priEntity.CreateDateTime!=null)
                {
                    sqlBudIns.Append("PriCreateDateTime,");
                    sqlBudValue.Append("@priCreateDateTime,");
                    sprList.Add(new SqlParameter("@priCreateDateTime", priEntity.CreateDateTime));
                    blState = true;
                }
                if (priEntity.ModifyUserID != -1)
                {
                    sqlBudIns.Append("PriModifyUserID,");
                    sqlBudValue.Append("@priModifyUserID,");
                    sprList.Add(new SqlParameter("@priModifyUserID", priEntity.ModifyUserID));
                    blState = true;
                }
                if (priEntity.ModifyDateTime!="" && priEntity.ModifyDateTime!=null)
                {
                    sqlBudIns.Append("PriModifyDateTime,");
                    sqlBudValue.Append("@priModifyDateTime,");
                    sprList.Add(new SqlParameter("@priModifyDateTime", priEntity.ModifyDateTime));
                    blState = true;
                }
                if (priEntity.RecordStatus != -1)
                {
                    sqlBudIns.Append("PriRecordStatus,");
                    sqlBudValue.Append("@priRecordStatus,");
                    sprList.Add(new SqlParameter("@priRecordStatus", priEntity.RecordStatus));
                    blState = true;
                }

                if (blState)
                {
                    string strSQL = sqlBudIns.ToString().Substring(0, sqlBudIns.Length - 1) + ")" + sqlBudValue.ToString().Substring(0, sqlBudValue.Length - 1) + ")";
                    SqlParameter[] values = sprList.ToArray();
                    //AppLog.Write("DAL-增加用户的SQL语句: " + strSQL, AppLog.LogMessageType.Debug);
                    flag = DAL_DBGM.InsertInfo(strSQL, values);
                }
                else
                {
                    //添加权限失败:没有任何数据.
                    AppLog.Write("添加权限失败:没有任何数据.", AppLog.LogMessageType.Info);
                }
                // insert into [PER_USER](UserLoginName,UserLoginPassword@loginName,@loginPassword
                return flag;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_SYS_Privilege.addPrivilege(Entity_SYS_Privilege priEntity)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }

        public bool updatePrivilegeByPrivilegeIDAndPrivilegeEntity(decimal priID, Entity_SYS_Privilege priEntity)
        {
            try
            {
                bool flag = false;
                bool blState = false;
                string strSQL = "update [SYS_PRIVILEGE] set";
                if (priEntity.PriMaster != "" && priEntity.PriMaster != null)
                {
                    strSQL += " PriMaster='" + priEntity.PriMaster + "',";
                    blState = true;
                }
                if (priEntity.PriMasterValue != -1)
                {
                    strSQL += " PriMasterValue=" + priEntity.PriMasterValue + ",";
                    blState = true;
                }
                if (priEntity.PriAccess != "" && priEntity.PriAccess != null)
                {
                    strSQL += " PriAccess='" + priEntity.PriAccess + "',";
                    blState = true;
                }
                if (priEntity.PriAccessValue != -1)
                {
                    strSQL += " PriAccessValue=" + priEntity.PriAccessValue + ",";
                    blState = true;
                }
                if (priEntity.PriOperation != "" && priEntity.PriOperation != null)
                {
                    strSQL += " PriOperation='" + priEntity.PriOperation + "',";
                    blState = true;
                }
                if (priEntity.CreateUserID != -1)
                {
                    strSQL += " PriCreateUserID=" + priEntity.CreateUserID + ",";
                    blState = true;
                }
                if (priEntity.CreateDateTime != "" && priEntity.CreateDateTime != null)
                {
                    strSQL += " PriCreateDateTime = '" + priEntity.CreateDateTime + "',";
                    blState = true;
                }
                if (priEntity.ModifyUserID != -1)
                {
                    strSQL += " PriModifyUserID=" + priEntity.ModifyUserID + ",";
                    blState = true;
                }
                if (priEntity.ModifyDateTime != "" && priEntity.ModifyDateTime != null)
                {
                    strSQL += " priModifyDateTime='" + priEntity.ModifyDateTime + "',";
                    blState = true;
                }
                if (priEntity.RecordStatus != -1)
                {
                    strSQL += " PriRecordstatus=" + priEntity.RecordStatus + ",";
                    blState = true;
                }
                if (blState)
                {
                    strSQL = strSQL.Substring(0, strSQL.Length - 1) + " where PriID=@priID";
                    SqlParameter[] values = new SqlParameter[]{
                        new SqlParameter("@priID",priID)
                    };
                    //AppLog.Write(strSQL, AppLog.LogMessageType.Debug);
                    flag = DAL_DBGM.UpdateInfo(strSQL, values);
                    //AppLog.Write(priID.ToString()+flag.ToString(), AppLog.LogMessageType.Debug);
                    return flag;
                }
                else
                {
                    //没有需要更新的内容.
                    return flag;
                }                
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_SYS_Privilege.updatePrivilegeByPrivilegeIDAndPrivilegeEntity(decimal priID, Entity_SYS_Privilege priEntity)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }

        public bool deletePrivilegeByPrivilegeID(decimal priID)
        {
            try
            {
                bool flag = false;
                string strSQL = "delete from [SYS_PRIVILEGE] where PriID=@priID";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@priID",priID)
                };
                flag = DAL_DBGM.DeleteInfo(strSQL, values);
                return flag;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_SYS_Privilege.deletePrivilegeByPrivilegeID(decimal priID)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }

        public bool deletePrivilegeByMasterAndMasterValue(string strMaster,decimal dclMasterValue)
        {
            try
            {
                bool flag = false;
                string strSQL = "delete from [SYS_PRIVILEGE] where PriMaster=@pmaster and PriMasterValue=@pvalue";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@pmaster",strMaster),
                    new SqlParameter("@pvalue",dclMasterValue)
                };
                flag = DAL_DBGM.DeleteInfo(strSQL, values);
                return flag;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_SYS_Privilege.deletePrivilegeByMasterAndMasterValue(string strMaster,decimal dclMasterValue)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }
    }
}
